<?php
require_once '../kelas/kategori.php';
require_once '../kelas/fungsi.php';

require_once '../kelas/produk.php';
$objProduk = new Produk();
$objKategori = new kategori();
$secure = new amankanParam();
$objFungsi=new fungsi();
$getMethod = $secure->getMethod();
$objKategori->getCombo();

?>
<style type="text/css">
.form-produk{
    height: 500px;

}
</style>
<h3>Produk</h3>
<form name="formProduk" action="../proses/produk.php?on=simpan" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="id" />
    <input type="hidden" name="gambar_awal" />
    <table class="form-produk" border="0">
        <tr>
            <td>Kategori</td>
            <td colspan="3"><select required="required" class="text_box_3" name="idkategori"><?php echo $objKategori->getCombo(); ?></select></td>        
            <td rowspan="8"><img style="width: 100%;height: 100%;border: 1px solid black" src="<?php echo BASEURL; ?>image/no_image_produk.jpg"></td>        
        </tr>
        <tr>
            <td>Nama</td>
            <td colspan="3"><input required="required" class="text_box_3" type="text" name="nama" placeholder="Nama Produk" /></td>        
        </tr>
        <tr>
            <td >Panjang</td>
            <td ><input class="text_box_100" type="text" name="panjang" placeholder="Panjang Produk" />cm.</td>        

            <td >Tinggi</td>
            <td ><input class="text_box_100"  type="text" name="tinggi" placeholder="Tinggi Produk" />cm.</td>        
        </tr>
        <tr>
        </tr>
        <tr>
            <td >Lebar</td>
            <td  ><input class="text_box_100"  type="text" name="lebar" placeholder="Lebar Produk" />cm.</td>        

            <td  >Berat</td>
            <td  ><input class="text_box_100"  type="text" name="berat" placeholder="Berat Produk" />cm.</td>        
        </tr>
        <tr>
        </tr>
        <tr>
            <td>Keterangan</td>
            <td colspan="3"><textarea class="text_box_3" required="required" name="keterangan" placeholder="Keterangan Produk" ></textarea></td>        
            
        </tr>
        <tr>
            <td >Harga Beli</td>
            <td ><input class="text_box_100" placeholder="Harga Beli"  type="text"  name="h_beli"></td>     
            <td >Harga Jual</td>
            <td ><input class="text_box_100" placeholder="Harga Jual"  type="text"  name="h_jual"></td>     
            <td>Gambar</td>
        </tr>
        <tr>
            <td >Aktif</td>
            <td colspan="3" ><input class="text_box_3"  type="checkbox" checked="checked" name="aktif" value="1"></td>     
            <td  ><input class="text_box_3"   required="required" type="file" name="gambar" placeholder="Gambar Produk" /></td> 
        </tr>
        <tr> 
            <td>Diskon</td>
            <td colspan="3"><input type="text" colspan="3" name="diskon" placeholder="Diskon"/>%</td>
            <td 
            style="text-align: center" >
            <input type="submit" name="submit" value="Simpan" /><input type="reset" value="Reset" /></td>        
        </tr>    
    </table>
</form>
<br/>
<div class="frame_table_order">
        <table width="100%" cellpadding="5" cellspacing="0" border="1" style="border-collapse:collapse">

    <thead>
            <tr align="center" valign="middle" bgcolor="#CCCCCC">
            <th>No</th>
            <th>Produk</th>
            <th>Kategori</th>
            <th>Dimensi(PxLxT)[cm]</th>
            <th>Keterangan</th>
            <th>Stok</th>
            <th>Aktif</th>
            <th>Pilihan</th>            
        </tr>
    </thead>
    <tbody>
        <?php
        $fung = new fungsi();
        $dtPerPage = 10;
        $start = isset($getMethod['hal']) ? $getMethod['hal'] : 0;
        $dataProd = $objProduk->getData($dtPerPage, $start * $dtPerPage);
        $no = 1 + ($start * $dtPerPage); //nanti bisa dinamis
        foreach ($dataProd['result'] as $vKat) {
            ?>
<tr class="font_isi_judul" align="center" valign="middle">
            <td><?php echo $no++; ?></td>
            <td><?php echo $vKat['nama']; ?></td>
            <td><?php echo $vKat['nama_kategori']; ?></td>
            <td><?php echo $vKat['panjang'] . ' x ' . $vKat['lebar'] . ' x ' . $vKat['tinggi']; ?> cm.</td>
            <td><?php echo strlen($vKat['keterangan'])>20?substr($vKat['keterangan'],0,17)."...":$vKat['keterangan']; ?></td>
            <td><?php echo (int)$vKat['jumlah']; ?></td>
            <td><a href="#" class="<?php echo ($vKat['aktif'] == 1) ? "iconActive" : "iconNonaktif"; ?>">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></td>
            <td><a href="#" 
             aktif="<?php echo $vKat['aktif'] ?>" 
             id_prod="<?php echo $vKat['id'] ?>" 
             class="aktifasi_produk option"><?php echo ($vKat['aktif'] == 1) ? "Nonaktifkan" : "Aktifkan"; ?></a>
             <a href="#"
             id_prod="<?php echo $vKat['id'] ?>"                        
             class="edit_produk option">Edit</a></td>
             </tr><?php
         }
         ?>
     </tbody>
     <tfoot>
        <tr><th colspan="9"><br/><?php
        echo $fung->buatHalaman($dataProd['total'], $dtPerPage, $start, '&v=master&p=produk');
        ?><br/></th></tr>
    </tfoot>
</table>
</div>
<script type="text/javascript">
$(document).ready(function(){
    var thisForm = $(document).find('form[name=formProduk]');
    thisForm.validate({
        rules:{
            idkategori:{
                required:true
            },
            nama:{
                minlength:3,
                required:true
            },
            panjang:{
                number:true
            },
            lebar:{
                number:true
            },
            tinggi:{
                number:true
            },
            berat:{
                number:true
            },
            lebar:{
                number:true
            },
            h_jual:{
                number:true,
                required:true
            },
            h_beli:{
                number:true,
                required:true
            },
            diskon:{
                number:true
            }
        }
    });
    $(document).find('form[name=formProduk]').find('input[type=reset]').click(function(e){
        var thisForm = $(document).find('form[name=formProduk]');

        thisForm.find('input[type=submit]').val('Simpan');            
        thisForm.find('img').attr('src',baseUrl+'image/no_image_produk.jpg');
        thisForm.find('input[type=file]').attr('required','required');
    });

    $('a.aktifasi_produk').click(function(e){
        var attrLink = e.target.attributes;
        var idKar = attrLink.getNamedItem('id_prod').value;
        var activeIt = attrLink.getNamedItem('aktif').value;
        $.ajax({
            url:baseUrl+'proses/produk.php?on=aktifasi',
            type:'POST',
            dataType:'JSON',
            data:{
                id:idKar,
                aktif:activeIt
            },
            success:function(e){
                if(e.success==false){
                    alert(e.error_msg);
                }
                else{
                    document.location = document.URL;
                }
            },
            error:function(e){
                alert('Gagal Mengubah Aktifasi Produk');
            }
        });
        e.preventDefault();
    });
    $('a.edit_produk').click(function(e){      
        var thisForm = $(document).find('form[name=formProduk]');           
        thisForm.find('input[type=submit]').attr('value','Update');
        var thisKat = this.attributes;
        var idProd= thisKat.getNamedItem('id_prod').value;
        $.ajax({
            type:'POST',
            url:baseUrl+'proses/produk.php?on=getForEdit',
            dataType:'JSON',
            data:{
                id_prod:idProd
            },
            success:function(resp){
                var dataResp = resp;
                if(resp.success==true){
                    var dataEdit = resp.data[0];
                    console.log(dataEdit);
                    thisForm.find('input[type=file]').removeAttr('required');
                    thisForm.find('img').attr('src',baseUrl+'produk/'+dataEdit.gambar);
                    thisForm.find('select[name=idkategori]').val(dataEdit.idkategori);
                    thisForm.find('input[name=gambar_awal]').val(dataEdit.gambar);
                    thisForm.find('input[name=panjang]').val(dataEdit.panjang);
                    thisForm.find('input[name=lebar]').val(dataEdit.lebar);
                    thisForm.find('input[name=nama]').val(dataEdit.nama);
                    thisForm.find('input[name=tinggi]').val(dataEdit.tinggi);
                    thisForm.find('input[name=h_beli]').val(dataEdit.h_beli);
                    thisForm.find('input[name=h_jual]').val(dataEdit.h_jual);
                    thisForm.find('input[name=diskon]').val(dataEdit.dis);
                    thisForm.find('input[name=berat]').val(dataEdit.berat);
                    thisForm.find('input[name=id]').val(idProd);
                    thisForm.find('textarea[name=keterangan]').val(dataEdit.keterangan);
                    thisForm.find('input[name=idkategori]').val(dataEdit.idkategori);
                    thisForm.find('input[name=aktif]').removeAttr('checked');

                    if(dataEdit.aktif==1){
                        thisForm.find('input[name=aktif]').click();
                    }
                }
                else{
                    alert('Gagal mengambil data');
                }
            },
            error:function(resp){
                console.log(resp);
            }
        });
thisProd = this.attributes;
console.log(thisProd);
e.preventDefault();
})
});

</script>